nodeflagperl = new Array();
for (i = 0; i < 80; i++) {
	nodeflagperl[i] = 0;
}

function selectOnes() {

}

function checkID() {
	if (document.myform.showid.value == "") {
		alert("Please choose a node at least!");
		return false;
	}
	var str = prompt("Please input password", "******");
	if (str != "tinyos") {
		alert("Wrong Password!");
		return false;
	}

	return true;
}

function checkpassword() {
	var str = prompt("Please input password", "******");
	if (str != "tinyos") {
		alert("Wrong Password!");
		return false;
	}
	return true;
}

function listenToNode(id){
	var xmlHttp = new XMLHttpRequest();
	if (xmlHttp == null) {
		alert("no support AJAX！");
	}
	var url = "begin_listen.jsp" + "?nodeid="+id;
	xmlHttp.onreadystatechange = function() {  
		if (xmlHttp.readyState == 4 && xmlHttp.status == 200) { //FIXME:
			alert("success to open!");
		}
	}
	xmlHttp.open("GET", url, true);
	xmlHttp.send(null);  //FIXME:when listen thread is running , then goto next page
	return true;
}

function opencomm1() {
	var xmlHttp = new XMLHttpRequest();
	if (xmlHttp == null) {
		alert("no support AJAX！");
	}
	var url = "opencomm_1.jsp";
	xmlHttp.onreadystatechange = function() {
		if (xmlHttp.readyState == 4 && xmlHttp.status == 200) {
			alert("success to open!");
		}
	}
	xmlHttp.open("GET", url, true);
	xmlHttp.send(null);
	return true;

}
function closecomm1() {
	var xmlHttp = new XMLHttpRequest();
	if (xmlHttp == null) {
		alert("no support AJAX！");
	}
	var url = "closecomm_1.jsp";

	xmlHttp.onreadystatechange = function() {
		if (xmlHttp.readyState == 4 && xmlHttp.status == 200) {
			// var response=xmlHttp.responseText;
			// alert(xmlHttp.responseText);
			// if(response=="success"){
			alert("success to close!");
			document.getElementById("closecomm").value = "CLOSED";
			// }
		}
	}
	xmlHttp.open("GET", url, true);
	xmlHttp.send(null);
	return true;
}

function set_dot(i, j)// when clicked ,change color of dots
{
	var c = document.getElementById("coordiv");
	var cxt = c.getContext("2d");
	cxt.fillStyle = "#00FF00";
	cxt.beginPath();
	cxt.arc(15 + i * 40, 15 + j * 40, 15, 0, Math.PI * 2, true);
	cxt.closePath();
	cxt.fill();
	cxt.fillStyle = "#000000";
	cxt.font = "16px Arial";
	cxt.fillText("" + (i * 10 + j), 15 + i * 40 - 7, 15 + j * 40 + 4);

}

function cancel_dot(i, j)// when clicked ,change color of dots
{
	var c = document.getElementById("coordiv");
	var cxt = c.getContext("2d");
	cxt.fillStyle = "#FF0000";
	cxt.beginPath();
	cxt.arc(15 + i * 40, 15 + j * 40, 15, 0, Math.PI * 2, true);
	cxt.closePath();
	cxt.fill();
	cxt.fillStyle = "#000000";
	cxt.font = "16px Arial";
	cxt.fillText("" + (i * 10 + j), 15 + i * 40 - 7, 15 + j * 40 + 4);
}

function show() // show init color
{
	var j;
	for (var i = 0; i < 8; i++) {
		for (j = 0; j < 10; j++) {
			var c = document.getElementById("coordiv");
			var cxt = c.getContext("2d");
			cxt.fillStyle = "#a9a9a9";
			cxt.beginPath();
			cxt.arc(15 + i * 40, 15 + j * 40, 15, 0, Math.PI * 2, true);
			cxt.closePath();
			cxt.fill();
			cxt.fillStyle = "#000000";
			cxt.font = "16px Arial";
			cxt.fillText("" + (i * 10 + j), 15 + i * 40 - 7, 15 + j * 40 + 4);
		}
	}
}
// /////////////////////////////////////tianjing//////
canvas_height = 320;
canvas_width = 600;
radius = canvas_width / 2;
x_change = new Array();
y_change = new Array();
x_change = [ 1.0, 0.742, 0.395, 0, 0.395, 0.742, 1.0 ];// dui x zuobiao zuo
// zhuan huan
y_change = [ 0.5, 0.766, 0.940, 1, 0.940, 0.766, 0.5 ];

var xynode = new Array();
var pianyi = 20;

var clear = 1;// mei ji shu dao 5 ,qing 0.

for (var m = 0; m < 35; m++) {
	xynode[m] = new Array();
	colnum = parseInt(m / 5);
	x_changed = parseFloat(x_change[colnum]);
	y_changed = parseFloat(y_change[colnum]);
	if (m < 20) {
		xynode[m][0] = radius * (1 - 0.2 * clear * x_changed) + pianyi;
	} else {
		xynode[m][0] = radius * (1 + 0.2 * clear * x_changed) + pianyi;
	}
	xynode[m][1] = canvas_height - radius * 0.2 * clear * y_changed;
	xynode[m][2] = m + 1 + 1;
	// alert("x:"+xynode[m][0]+"y:"+xynode[m][1]);
	if (clear == 5) {
		clear = 0;
	}
	clear++;
}

var sink = [ radius + pianyi, canvas_height - 10 ];
function draw_sink() {
	var c = document.getElementById("coordiv");
	var cxt = c.getContext("2d");
	cxt.fillStyle = "#a9a9a9";
	cxt.beginPath();
	cxt.arc(sink[0], sink[1], 14, 0, Math.PI * 2, true);
	cxt.closePath();
	cxt.fill();
	cxt.fillStyle = "#000000";
	cxt.font = "16px Arial";
	cxt.fillText("" + 1, radius + pianyi - 5, canvas_height - 10 + 4);
}

function set_dot_tianjing(id) {
	var c = document.getElementById("coordiv");
	var cxt = c.getContext("2d");
	var x = parseInt(xynode[id][0]);
	var y = parseInt(xynode[id][1]);
	var num = parseInt(xynode[id][2]);
	cxt.fillStyle = "#00FF00";
	cxt.beginPath();
	if (id % 5 == 0) {
		cxt.font = "12px Arial";
		cxt.arc(x, y, 10, 0, Math.PI * 2, true);
	} else {
		cxt.font = "16px Arial";
		cxt.arc(x, y, 16, 0, Math.PI * 2, true);
	}
	cxt.closePath();
	cxt.fill();
	cxt.fillStyle = "#000000";
	cxt.fillText("" + num, x - 8, y + 4);
}

function show_tianjing() {
	var c = document.getElementById("coordiv");
	var cxt = c.getContext("2d");
	draw_sink();
	// col1:
	for (var m = 0; m < 35; m++) {
		var x = parseInt(xynode[m][0]);
		var y = parseInt(xynode[m][1]);
		var num = parseInt(xynode[m][2]);
		cxt.fillStyle = "#a9a9a9";
		cxt.beginPath();
		if (m % 5 == 0) {
			cxt.font = "12px Arial";
			cxt.arc(x, y, 10, 0, Math.PI * 2, true);
		} else {
			cxt.font = "16px Arial";
			cxt.arc(x, y, 16, 0, Math.PI * 2, true);
		}
		cxt.closePath();
		cxt.fill();
		cxt.fillStyle = "#000000";
		cxt.fillText("" + num, x - 8, y + 4);
	}
}

function isSinkClicked(x, y) {
	console.log("node " + m + " : " + x + "," + y + ";" + sink[0] + ","
			+ sink[1] + "\n");

	tmpx = sink[0] - window.pageXOffset;
	tmpy = sink[1] - window.pageYOffset;
	console.log("r:" + (Math.pow(x - tmpx, 2) + Math.pow(y - tmpy, 2)) + ";"
			+ Math.pow(16, 2) + "\n");
	if (tmpx < 0 || tmpy < 0) {
		return false;
	}

	if ((Math.pow(x - tmpx, 2) + Math.pow(y - tmpy, 2)) <= Math.pow(16, 2)) {
		return true;
	}
	return false;
}

function whichNodeClicked() {
	for (var m = 0; m < 35; m++) {
		var tmpx = parseInt(xynode[m][0]) - window.pageXOffset;
		var tmpy = parseInt(xynode[m][1]) - window.pageYOffset;
		if (m % 5 == 0) {
			tmpradius = 10;
		} else {
			tmpradius = 16;
		}
		if (((x - tmpx) * (x - tmpx) + (y - tmpy) * (y - tmpy)) <= tmpradius
				* tmpradius) {
			break;
		}
	}

	if (m == 35) {
		m = -1;
	}
	return m;
}

function sensor_show_tianjing(e) // when clicked ,get i,j
{
	x = e.clientX - 150;
	y = e.clientY - 80;
	var nodeid = -1;
	var tmpradius = 0;

	if (isSinkClicked(x, y)) {
		nodeid = 0;
	} else {
		var m = whichNodeClicked(x, y);
		if (m >= 0) {
			nodeid = m + 1;
		}
	}

	if (nodeid >= 0) {
		//first listen to the serial port so we can store data to database
		listenToNode(nodeid);
		//then read from the database and show on pages
		document.getElementById("search").value = nodeid;
		var formObj = document.getElementById("showsensor");
		console.log("node " + nodeid + " clicked\n");
		formObj.submit();
	}
}

function sensor_show(e) // when clicked ,get i,j
{
	x = e.clientX - 150;
	y = e.clientY - 80;
	var nodeid = -1;
	for (var i = 0; i < 8; i++) {
		for (var j = 0; j < 10; j++) {
			tmpx = 15 + 40 * i;
			tmpy = 15 + 40 * j;
			if (((x - tmpx) * (x - tmpx) + (y - tmpy) * (y - tmpy)) <= 15 * 15) {
				nodeid = i * 10 + j;
				if (nodeflagperl[nodeid] == 1) {
					nodeflagperl[nodeid] = 0;
					cancel_dot(i, j);
				} else if (nodeflagperl[nodeid] == 0) {
					nodeflagperl[nodeid] = 1;
					set_dot(i, j);
				}

				break;
			}
		}

	}
	if ((nodeid >= 0) && (nodeid != 79) && (nodeid != 78)) {
		document.getElementById("search").value = nodeid;
		var formObj = document.getElementById("showsensor");
		formObj.submit();
	}
}
function cnvs_clearCoordinates() // when leave are ,clear x,y
{
	document.getElementById("xycoordinates").innerHTML = "";
}
function printData(e) {

}
